package cn.edu.sgu.www.mhxysy.service.xingyin.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.xingyin.XingyinSkillCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.xingyin.XingyinSkillCategory;
import cn.edu.sgu.www.mhxysy.mapper.xingyin.XingyinSkillCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.xingyin.XingyinSkillCategoryPager;
import cn.edu.sgu.www.mhxysy.service.xingyin.XingyinSkillCategoryService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class XingyinSkillCategoryServiceImpl implements XingyinSkillCategoryService {

    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final XingyinSkillCategoryMapper xingyinSkillCategoryMapper;

    @Autowired
    public XingyinSkillCategoryServiceImpl(
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            XingyinSkillCategoryMapper xingyinSkillCategoryMapper) {
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.xingyinSkillCategoryMapper = xingyinSkillCategoryMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_XINGYIN_SKILL;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(XingyinSkillCategoryDTO xingyinSkillCategoryDTO) {
        XingyinSkillCategory xingyinSkillCategory = toEntity(xingyinSkillCategoryDTO);

        xingyinSkillCategory.setId(null);

        xingyinSkillCategoryMapper.insert(xingyinSkillCategory);
    }

    @Override
    public void deleteById(Integer id) {
        xingyinSkillCategoryMapper.deleteById(id);
    }

    @Override
    public void updateById(XingyinSkillCategoryDTO xingyinSkillCategoryDTO) {
        ObjectUtils.requireNonNull(xingyinSkillCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

        XingyinSkillCategory xingyinSkillCategory = toEntity(xingyinSkillCategoryDTO);

        xingyinSkillCategoryMapper.updateById(xingyinSkillCategory);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<XingyinSkillCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                xingyinSkillCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public Page<XingyinSkillCategory> selectByPage(XingyinSkillCategoryPager pager) {
        QueryWrapper<XingyinSkillCategory> queryWrapper = new QueryWrapper<>();
        Page<XingyinSkillCategory> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getType() != null,
                "type", pager.getType()
        );
        queryWrapper.eq(
                pager.getSchoolId() != null,
                "school_id", pager.getSchoolId()
        );

        return xingyinSkillCategoryMapper.selectPage(page, queryWrapper);
    }

    private XingyinSkillCategory toEntity(XingyinSkillCategoryDTO xingyinSkillCategoryDTO) {
        XingyinSkillCategory xingyinSkillCategory = new XingyinSkillCategory();

        xingyinSkillCategory.setId(xingyinSkillCategoryDTO.getId());
        xingyinSkillCategory.setName(xingyinSkillCategoryDTO.getName());
        xingyinSkillCategory.setType(xingyinSkillCategoryDTO.getType());
        xingyinSkillCategory.setNote(xingyinSkillCategoryDTO.getNote());
        xingyinSkillCategory.setSchoolId(xingyinSkillCategoryDTO.getSchoolId());

        return xingyinSkillCategory;
    }

}